package com.blt.car.me.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.IBaseAdapter;
import com.blt.car.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import blt.app_core.api.base.CoreManager;
import blt.app_core.api.journey.bean.TravelOrder;
import blt.app_core.api.login.LoginImpl;
import blt.app_core.api.other.OutApi;

/**
 * Created by xgp on 2017/7/5.
 */

public class BillJourneyAdapter extends IBaseAdapter {
    private SparseArray<Boolean> sparseArray;
    private boolean isSelectAll;


    public BillJourneyAdapter(Context context, int resource) {
        super(context, resource);
        sparseArray = new SparseArray<>();
    }

    public void init() {
        for (int i = 0; i < list.size(); i++) {
            sparseArray.put(i, false);
        }
    }

    @Override
    public View getYView(final int i, View view, ViewGroup viewGroup) {
        TextView startSite = (TextView) view.findViewById(R.id.bill_journey_from_site);
        TextView endSite = (TextView) view.findViewById(R.id.bill_journey_finish_site);
        TextView startTime = (TextView) view.findViewById(R.id.bill_journey_from_time);
        TextView endTime = (TextView) view.findViewById(R.id.bill_journey_finish_time);
        TextView amount = (TextView) view.findViewById(R.id.bill_journey_amount);
        CheckBox checkBox = (CheckBox) view.findViewById(R.id.bill_journey_cb);

        TravelOrder order = (TravelOrder) getItem(i);
        String start = order.start.split("-")[1];
        startSite.setText(start);
        String end = order.end.split("-")[1];
        endSite.setText(end);
        startTime.setText(StringUtils.getDay(order.startTime));
        endTime.setText(order.carNo);
        amount.setText(order.amount + "元");

        checkBox.setChecked(isSelectAll);

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sparseArray.put(i, isChecked);
                CoreManager.notifyClients(OutApi.class, OutApi.onSelect);
            }
        });

        return view;
    }

    /**
     * 全选
     *
     * @param isSelect
     */
    public void selectAll(boolean isSelect) {
        isSelectAll = isSelect;
        for (int i = 0; i < list.size(); i++) {
            sparseArray.put(i, true);
        }
        notifyDataSetChanged();
    }

    public List<TravelOrder> getSelectCount() {
        List<TravelOrder> orders = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            TravelOrder order = (TravelOrder) list.get(i);
            if (sparseArray.get(i)) {
                orders.add(order);
            }
        }
        return orders;
    }


    public ArrayList<Integer> getSelectItem() {
        ArrayList<Integer> ids = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            TravelOrder order = (TravelOrder) list.get(i);
            if (sparseArray.get(i)) {
                ids.add(order.id);
            }
        }
        return ids;
    }

    /**
     * 单选
     */
//    public int getSelectItem() {
//        int travelId = 0;
//        for (int i = 0; i < sparseArray.size(); i++) {
//            if (sparseArray.get(i)) {
//                TravelOrder order = (TravelOrder) list.get(i);
//                travelId = order.id;
//            }
//        }
//        return travelId;
//    }


}
